/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package stats;

import java.sql.*;
import java.util.*;
import java.io.*;

import com.mchange.v2.c3p0.*;
/**
 *
 * @author balbir kaur
 */
public class FacebookDatabase {
    public static ComboPooledDataSource cpds=null;
    private Connection con=null;
    public static void initialize (){
        try{
        cpds=new ComboPooledDataSource();
        Properties props=new Properties();
        FileInputStream propfile=null;
        try{
         String fs=System.getProperty("file.separator");
        String homedir=System.getProperty("user.home");
        propfile=new FileInputStream(homedir+fs+"quiz.properties");
        props.load(propfile);
        cpds.setDriverClass(props.getProperty("driverclass"));
        cpds.setJdbcUrl(props.getProperty("jdbcurl"));
        cpds.setUser(props.getProperty("dbuser"));
        cpds.setPassword(props.getProperty("dbpassword"));
        cpds.setMinPoolSize(Integer.parseInt(props.getProperty("minpoolsize")));
        cpds.setMaxPoolSize(Integer.parseInt(props.getProperty("maxpoolsize")));
        propfile.close();
    }
    catch(IOException e){
    System.out.println("Error the file does not exist"+e.getMessage());
    }
        finally{
        if(propfile!=null){
            try{
            propfile.close();}
            catch(Exception e){
            }
        }

        }
        }
     catch(Exception ex){
             System.out.println(ex.getMessage());
        }
   

    }
        
    
    public void openConnection(){
       try{
           con=cpds.getConnection();
       }
       catch(Exception sql){
           System.out.println("che");
       }
       }
    
public boolean checkUser(Long uid){
String query="Select * from facebookuser where id="+uid;
boolean flag=false;
try{
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(query);
while(rs.next()){
flag=true;
}
rs.close();
}
catch(SQLException e){
System.out.println(e.getMessage());
}
return flag;
}
public boolean insertUser(Long uid,Double perc){
String query="Insert into facebookuser values("+uid+","+perc+")";
boolean flag=false;
try{
Statement stmt=con.createStatement();
stmt.executeUpdate(query);
flag=true;
}
catch(SQLException e){
}
return flag;
}
public boolean updateUser(Long uid,Double perc){
boolean flag=false;
String query="update facebookuser set percentage="+perc+"where uid="+uid;
try{
Statement stmt=con.createStatement();
stmt.executeUpdate(query);
flag=true;
}
catch(SQLException e){
}
return flag;
}
  public void closeConnection(){
     try{         
     if(con!=null)
      con.close();
  }
catch(SQLException e){
    System.out.println("hmmmoooooo");
}
  }

public Double getUserPercentage(Long id){
    String query="Select percentage from facebookuser where id="+id;
   Double perc=null;
    try{
    Statement stmt=con.createStatement();
    ResultSet rs=stmt.executeQuery(query);
    while(rs.next()){
    perc=rs.getDouble("percentage");}
    rs.close();
    }
   catch(Exception e){
    System.out.println(e.getMessage());
   }
    return perc;

}
public void destroy(){
   try{
    DataSources.destroy(cpds);
   }
   catch(SQLException e){
   System.out.println();
   }
}

}
